package cj.web.tel.controllers;

import java.text.ParseException;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.admin.utils.DateUtils;
import cj.web.login.controllers.BaseController;
import cj.web.tel.constants.GroupPeopleTypeEnum;
import cj.web.tel.domain.FloorInfor;
import cj.web.tel.domain.GroupDep;
import cj.web.tel.domain.GroupInfor;
import cj.web.tel.domain.GroupOffices;
import cj.web.tel.domain.GroupPeople;
import cj.web.tel.service.FloorInforService;
import cj.web.tel.service.GroupDepService;
import cj.web.tel.service.GroupInforService;
import cj.web.tel.service.GroupOfficesService;
import cj.web.tel.service.GroupPeopleService;

@Path("/groupPeopleGl")
public class GroupPeopleGLController extends BaseController{

	private static Logger logger = Logger.getLogger(GroupPeopleGLController.class.getName());
	
	@Autowired
	private GroupPeopleService groupPeopleService;
	@Autowired
	private GroupDepService groupDepService;
	@Autowired
	private GroupOfficesService groupOfficesService;
	@Autowired
	private FloorInforService floorInforService;
	@Autowired
	private GroupInforService groupInforService;
	
	
	@Get("show")
	public String show(Model model) {
		List<GroupInfor> groupInforlis = groupInforService.queryAllNotPid();
		model.add("groupInforlis", groupInforlis);
		return "/views/tel/groupPeopleGlList.vm";
	}
	
	@Get("showDep")
	public String showDep(Model model,@Param("groupId") int groupId) {
		JSONObject jo = new JSONObject();
		List<GroupDep> groupDeplis = groupDepService.queryByGroupId(groupId);
		jo.put("groupDeplis", groupDeplis);
		return "@"+jo.toString();
	}
	
	@Get("showOffice")
	public String showOffice(Model model,@Param("depId") int depId) {
		JSONObject jo = new JSONObject();
		List<GroupOffices> groupOfficeslis = groupOfficesService.queryAllById(depId);
		jo.put("groupOfficeslis", groupOfficeslis);
		return "@"+jo.toString();
	}
	
	@Post("getList")
	public String queryList(GroupPeople groupPeople) {
	
		//当前页  
		groupPeople.setNameOrderNo(0);
		groupPeople.setStart((groupPeople.getPage()-1)*groupPeople.getRows());
		groupPeople.setLength(groupPeople.getRows());
		
		JSONObject jo = new JSONObject();
		
		Map rep = groupPeopleService.queryPeopleGl(groupPeople);
		
		List<GroupPeople> objs = (List<GroupPeople>) rep.get("groupPeoplelis");
		for(GroupPeople obj:objs){
			obj.setTypeName(GroupPeopleTypeEnum.getName(obj.getType()));
		}
		
		int size = (int) rep.get("size");
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id) throws ParseException{
		model.add("groupPeopleTypeEnum",EnumSet.allOf(GroupPeopleTypeEnum.class));
		GroupPeople groupPeople = groupPeopleService.queryById(id);
		model.add("groupPeople", groupPeople);
		List<FloorInfor> floorInforlis = floorInforService.queryByAll();
		model.add("floorInforlis", floorInforlis);
		List<GroupOffices> officelis = groupOfficesService.queryAllById(groupPeople.getDepId());
		model.add("officelis", officelis);
		
		return "/views/tel/groupPeopleGLEdit.vm";
	}
	
	@Get("adjustment")
	public String adjustment(Model model,@Param("Ids") String Ids) throws ParseException{
		List<GroupInfor> groupInforlis = groupInforService.queryAllNotPid();
		model.add("groupInforlis", groupInforlis);
		model.add("Ids", Ids);
		List<FloorInfor> floorInforlis = floorInforService.queryByAll();
		model.add("floorInforlis", floorInforlis);
		return "/views/tel/groupPeopleGLEditJG.vm";
	}
	
	@Post("adjustmentById")
	public String adjustmentById(Model model,@Param("Ids") String Ids,@Param("groupId") int groupId,
			@Param("depId") int depId,@Param("officeId") int officeId,@Param("location") String location){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			groupPeopleService.update(groupId, officeId, depId, location, Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
	
	@Post("update")
	public String update(Model model,GroupPeople groupPeople,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		groupPeople.setUpdateAt(DateUtils.getNowTime());
		groupPeople.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			groupPeopleService.update(groupPeople);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			groupPeopleService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}